package swing;

import java.awt.Desktop;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import carlyang.Out;

public class DesktopDemo {
	private static Desktop desktop;

	public DesktopDemo() {
		// TODO Auto-generated constructor stub
	}

	/*
	 * 创建UI界面
	 */
	private static void constructGUI() {
		constructMenuGUI();
		constructTrayGUI();
	}

	/*
	 * 创建菜单
	 */
	private static void constructMenuGUI() {
		if (Desktop.isDesktopSupported()) {
			desktop = Desktop.getDesktop();
			Out.aprintln("Desktop class is supported");
		} else {
			Out.aprintln("Desktop class is not supported");
			System.exit(1);
		}
		JMenuItem openItem;
		JMenuItem editItem;
		JMenuItem printItem;
		JMenuItem browseToItem;
		JMenuItem emailToItem;

		JMenu fileMenu = new JMenu("File", false);
		JMenu mailMenu = new JMenu("Email", true);
		JMenu browseMenu = new JMenu("Browser", true);

		openItem = new JMenuItem("Open");
		openItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JFileChooser chooser = new JFileChooser();
				if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
					try {
						desktop.open(chooser.getSelectedFile());// .getAbsoluteFile()
					} catch (Exception e2) {
						// TODO: handle exception
						e2.printStackTrace();
					}
				}
			}
		});
		fileMenu.add(openItem);

		editItem = new JMenuItem("Edit");
		editItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JFileChooser chooser = new JFileChooser();
				if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
					try {
						desktop.edit(chooser.getSelectedFile());// .getAbsoluteFile()
					} catch (Exception e2) {
						// TODO: handle exception
						e2.printStackTrace();
					}
				}
			}
		});
		fileMenu.add(editItem);

		printItem = new JMenuItem("Print");
		printItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JFileChooser chooser = new JFileChooser();
				if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
					try {
						desktop.print(chooser.getSelectedFile());// .getAbsoluteFile()
					} catch (Exception e2) {
						// TODO: handle exception
						e2.printStackTrace();
					}
				}
			}
		});
		fileMenu.add(printItem);

		browseToItem = new JMenuItem("Go to www.baidu.com");
		browseToItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					// 两种方法选其一即可
					// URI browseURI = new URI("http://www.baidu.com");
					// desktop.browse(browseURI);
					Runtime.getRuntime()
							.exec("rundll32 url.dll,FileProtocolHandler http://www.baidu.com");
				} catch (Exception e2) {
					// TODO: handle exception
					Out.aprintln(e2.getMessage());
				}
			}
		});
		browseMenu.add(browseToItem);

		emailToItem = new JMenuItem("Email to 839893211@qq.com");
		emailToItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					URI mailtoURI = new URI("mailto:839893211@qq.com");
					desktop.mail(mailtoURI);
				} catch (Exception e2) {
					// TODO: handle exception
					Out.aprintln(e2.getMessage());
				}
			}
		});
		mailMenu.add(emailToItem);

		JMenuBar jMenuBar = new JMenuBar();
		jMenuBar.add(fileMenu);
		jMenuBar.add(browseMenu);
		jMenuBar.add(mailMenu);

		JFrame frame = new JFrame();
		frame.setTitle("Desktop helpr Applications");
		frame.setBounds(500, 200, 300, 200);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setJMenuBar(jMenuBar);
		frame.setVisible(true);
	}

	/*
	 * 创建托盘
	 */
	private static void constructTrayGUI() {
		if (!SystemTray.isSupported()) {
			Out.aprintln("SystemTray is not supported");
			System.exit(1);
		} else
			Out.aprintln("SystemTray is supported");
		SystemTray tray = SystemTray.getSystemTray();
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Image image = toolkit.getImage("F:/Android/adt_workspace/test/icon/trayIcon.png");

		// 创建托盘的弹出菜单
		PopupMenu menu = new PopupMenu();
		// 子菜单项1
		MenuItem messageItem = new MenuItem("Show Message");
		messageItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JOptionPane.showMessageDialog(null, "by carlyang");
			}
		});
		menu.add(messageItem);
		// 子菜单项2
		MenuItem closeItem = new MenuItem("Close");
		closeItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		menu.add(closeItem);

		TrayIcon icon = new TrayIcon(image, "SystemTray Demo", menu);
		icon.setImageAutoSize(true);
		try {
			tray.add(icon);
		} catch (Exception e) {
			// TODO: handle exception
			System.err.println("Could not add tray icon to system tray");
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				constructGUI();// 使用static方法或创建对象
			}
		});
	}

}
